package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.model.House;
import com.wuming.house.model.Houseprice;
import com.wuming.house.model.Secondhouseprice;
import com.wuming.house.service.HouseService;
import com.wuming.house.service.HousepriceService;
import com.wuming.house.service.SecondhousepriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/8/27 18:51
 * @Description:
 */
@Controller
@RequestMapping("/web/secondhouseprice")
public class SecondhousepriceController extends BaseController {

    @Autowired
    private SecondhousepriceService secondhousepriceService;

    @Autowired
    private HouseService houseService;

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list() {
        logger.info("查询二手房房价列表.");
        try {
            List<Secondhouseprice> houseprices = secondhousepriceService.getSecondhousepriceByParams(null, "", null, null);
            return CommonResult.createSuccessResult(houseprices, houseprices.size(), "查询楼盘二手房房价成功");
        } catch (Exception e) {
            logger.error("查询楼盘二手房房价异常,信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询楼盘二手房房价失败");
        }
    }


    @RequestMapping("/toAddSecondhouseprice")
    public String toAddSecondhouseprice() {
        return "/house/editSecondhouseprice";
    }

    @RequestMapping("/toEditSecondhouseprice/{id}")
    public String toEditSecondhouseprice(@PathVariable Long id, Model model) {
        Secondhouseprice houseprice = secondhousepriceService.getSecondhousepriceById(id);
        model.addAttribute("houseprice", houseprice);
        return "/house/editSecondhouseprice";
    }

    @ResponseBody
    @RequestMapping(value = "/save")
    public CommonResult save(Secondhouseprice houseprice) {
        if (null == houseprice.getId()) {
            houseprice.setCreateDatetime(new Date());
            secondhousepriceService.saveSecondhouseprice(houseprice);
            return CommonResult.createSuccessResult(houseprice, 1, "保存二手房价成功");
        } else {
            secondhousepriceService.modifySecondhouseprice(houseprice);
            return CommonResult.createFailureResult("保存二手房价失败");
        }
    }

}
